﻿Imports IntoNails.DataAccess
Imports IntoNails.Models
Imports IntoNails.My.Resources

Namespace ViewModel
    Public Class LoginViewModel
        Inherits SubWorkspaceViewModel

#Region "Declarations"

        Private ReadOnly _users As UserRepository

#End Region

#Region "Constructors"
        Public Sub New(ByVal repository As UserRepository)
        	Me.DisplayName = StringResources.INTONAILS_LOGIN_VIEWMODEL_DISPLAYNAME
        	
            _users = repository
        End Sub
#End Region

#Region "Delegate and Event"

        Public Event LoginSucceed As LoginCallback

        Protected Overridable Sub OnLoginSucceed(ByVal e As UserLoginEventArgs)
            Dim handler As LoginCallback = Me.LoginSucceedEvent
            If handler IsNot Nothing Then
                handler.Invoke(Me, e)
            End If
        End Sub

        Public Event LoginCancelled As EventHandler

        Protected Overridable Sub OnLoginCancelled(ByVal e As EventArgs)
            Dim exitHandler As EventHandler = Me.LoginCancelledEvent
            If exitHandler IsNot Nothing Then
                exitHandler.Invoke(Me, e)
            End If
        End Sub

#End Region

#Region "Properties"

        Private _username As String
        Public Property Username() As String
            Get
                Return _username
            End Get
            Set(ByVal value As String)
                If _username = value Then
                    Return
                End If

                _username = value
                MyBase.OnPropertyChanged("Username")
            End Set
        End Property

        Private _password As String
        Public Property Password() As String
            Get
                Return _password
            End Get
            Set(ByVal value As String)
                If _password = value Then
                    Return
                End If

                _password = value
                MyBase.OnPropertyChanged("Password")
            End Set
        End Property

        Private _message As String
        Public Property Message() As String
            Get
                Return _message
            End Get
            Set(ByVal value As String)
                _message = value
                Me.OnPropertyChanged("Message")
            End Set
        End Property

#End Region

#Region "Commands"

        Private _loginCommand As ICommand
        Public ReadOnly Property LoginCommand() As ICommand
            Get
                If _loginCommand Is Nothing Then
                    _loginCommand = New SimpleCommand(AddressOf Me.Login)
                End If
                Return _loginCommand
            End Get
        End Property

        Private _exitCommand As ICommand
        Public ReadOnly Property ExitCommand() As ICommand
            Get
                If _exitCommand Is Nothing Then
                    _exitCommand = New SimpleCommand(AddressOf Me.ExecuteExit)
                End If
                Return _exitCommand
            End Get
        End Property

#End Region

#Region "Functions"

        Private Sub Login()
            Dim dummy As User = _users.Login(Me.Username, Me.Password)
            If dummy Is Nothing Then
                Me.Password = String.Empty
                Me.Message = StringResources.LOGIN_FAILED_MESSAGE
                Return
            End If

            ' fires the login's event to the main view-model.
            Dim args As UserLoginEventArgs = New UserLoginEventArgs(dummy)
            Me.OnLoginSucceed(args)
        End Sub

        Private Sub ExecuteExit()
            Me.OnLoginCancelled(EventArgs.Empty)
        End Sub

#End Region

    End Class
End Namespace
